<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

class StoreVerificationClerk extends Model
{
    protected $table = 'ims_yz_store_verification_clerk';
    
    protected $fillable = [
        'uniacid',
        'uid',
        'store_id',
        'realname',
        'mobile',
        'status',
        'created_at',
        'updated_at',
        'deleted_at'
    ];

    protected $casts = [
        'uniacid' => 'integer',
        'uid' => 'integer',
        'store_id' => 'integer',
        'status' => 'boolean',
        'created_at' => 'integer',
        'updated_at' => 'integer',
        'deleted_at' => 'integer'
    ];

    /**
     * 获取门店
     */
    public function store(): BelongsTo
    {
        return $this->belongsTo(Store::class, 'store_id', 'id');
    }

    /**
     * 获取用户
     */
    public function user(): BelongsTo
    {
        return $this->belongsTo(User::class, 'uid', 'id');
    }

    /**
     * 查询启用的验证店员
     */
    public function scopeEnabled($query)
    {
        return $query->where('status', 1);
    }

    /**
     * 查询禁用的验证店员
     */
    public function scopeDisabled($query)
    {
        return $query->where('status', 0);
    }

    /**
     * 根据门店查询验证店员
     */
    public function scopeByStore($query, int $storeId)
    {
        return $query->where('store_id', $storeId);
    }

    /**
     * 根据用户查询验证店员
     */
    public function scopeByUser($query, int $uid)
    {
        return $query->where('uid', $uid);
    }

    /**
     * 获取状态文本
     */
    public function getStatusTextAttribute(): string
    {
        return $this->status ? '启用' : '禁用';
    }

    /**
     * 检查是否为启用状态
     */
    public function isEnabled(): bool
    {
        return $this->status == 1;
    }

    /**
     * 检查是否为禁用状态
     */
    public function isDisabled(): bool
    {
        return $this->status == 0;
    }

    /**
     * 启用验证店员
     */
    public function enable(): bool
    {
        return $this->update(['status' => 1]);
    }

    /**
     * 禁用验证店员
     */
    public function disable(): bool
    {
        return $this->update(['status' => 0]);
    }
}
